<?php 

require('inc/db.php');

// 获取总记录数
$sql = "select count(*) from house";
$result = mysql_query($sql, $conn);
$temp = mysql_fetch_array($result, MYSQL_NUM);

$total = $temp[0];

$total_page = (int)(($total + 11) / 12);

if( isset($_GET['page']) ) {
	$page = $_GET['page'];
	$start_row = ( ($page - 1) * 12);
}
else {
	$page = 1;
	$start_row = 0;
}
	
// 组装插入sql
//$sql = "select * from house order by created desc limit $start_row,10";
$sql = "select * from house limit $start_row,12";

// 执行mysql查询
$result = mysql_query($sql, $conn);


?>
<?php include('header.php'); ?>

<div class="wrapper">
	<div class="main">
		<div class="jpfy">
			<div class="title">精品房源</div>
			<div class="pic_bottom">
          		<ul>
				<?php 
                      for($i=0; $i<mysql_num_rows($result); $i++) {
	                  $house = mysql_fetch_array( $result, MYSQL_ASSOC );
                ?>
                	<li>
                 		<a href="house.php?id=<?php echo $house['id'] ?>">
                 			<img src="<?php echo $house['picture_path'] ?>" width="180" height="144" />
                 		</a>
                 		<p>
                 			<a href="house.php?id=<?php echo $house['id'] ?>"><?php echo $house['name'] ?></a>
                 		</p>
               		</li>
                <?php 
                     }
                ?>
                </ul>
          	
          		<div class="c"></div>
          		
          		<div class="blank3"></div>
          		
          		<div class="pagation">
          		<?php

          			$pre = $page-1;
          			
          			if( $pre > 1 ){
          				echo "<a href=\"house_list.php?page=1\">首页</a>";
          				echo "<a href=\"house_list.php?page=$pre\">上一页</a>";
          			}
          			
          			$start_point = $page - 5;
          			if( $start_point < 1 ) {
          				$start_point = 1;
          			} 
					for( $i=$start_point; $i<$page; $i++ ) {
						echo "<a href=\"house_list.php?page=$i\">$i</a>";
					}
					
					echo "<span>$page</span>";
          			
					$start_point = $page + 1;
					$end_point = $page + 5;
					if( $end_point > $total_page ) {
						$end_point = $total_page;
					}
					for( $i=$start_point; $i<=$end_point; $i++ ) {
						echo "<a href=\"house_list.php?page=$i\">$i</a>";
					}
					
					$next = $page + 1;
					if( $next <= $total_page ) {
						echo "<a href=\"house.php?page=$next\">下一页</a>";
						echo "<a href=\"house_list.php?page=$total_page\">尾页</a>";	
					}
          		?>
          		</div>
          		
          		<div class="blank3"></div>
          		
        	</div>
         </div>
    </div>
</div>

<?php include('footer.php') ?>
